//
//  LDCutPictureViewController.m
//  指乎
//
//  Created by Dean on 2016/12/5.
//  Copyright © 2016年 Dean. All rights reserved.
//

#import "LDCutPictureViewController.h"
#import "HXImageCropper.h"
@interface LDCutPictureViewController ()

@property (nonatomic,strong) HXImageCropper *cropperView;

@end

@implementation LDCutPictureViewController
-(instancetype)initWithSourceImage:(UIImage *)image {
    if (self = [super init]) {
        self.sourceImage = image;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _cropperView = [[HXImageCropper alloc] initWithCropImage:_sourceImage cropSize:CGSizeMake(SCREEN_WIDTH, (SCREEN_WIDTH)/2)];
    [self.view addSubview:_cropperView];

    self.view.backgroundColor = [UIColor blackColor];
    UIButton* cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel setTitle:@"取消" forState:0];
    [cancel setTitleColor:[UIColor whiteColor] forState:0];
    [cancel setFrame:CGRectMake(RatioWidth(10), SCREEN_HEIGHT-RatioHeight(60), 44, 44)];
    [cancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancel];
    
    
    
    UIButton* save = [UIButton buttonWithType:UIButtonTypeCustom];
    [save setTitle:@"确定" forState:0];
    [save setTitleColor:[UIColor whiteColor] forState:0];
    [save setFrame:CGRectMake(SCREEN_WIDTH - RatioWidth(10)-44, SCREEN_HEIGHT-RatioHeight(60), 44, 44)];
    [self.view addSubview:save];
    [save addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)saveAction:(id)sender{
    if (_completion) {
        _completion(self,[_cropperView getCroppedImage]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)cancelAction:(id)sender{
    
    UIImagePickerController *picker = (UIImagePickerController *)self.navigationController;
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
//    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
